package com.junjie.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junjie.entity.RCondition;
import com.junjie.services.RConditionService;
import com.junjie.utils.CommonParams;
import com.junjie.utils.CommonResult;
import com.junjie.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//房间情况信息表
@Controller
@RequestMapping("/rCondition")
@ResponseBody
public class RConditionController {

    @Autowired
    private RConditionService rConditionService;

    //万能查询接口
    @RequestMapping(value = "/getRConditions", method = RequestMethod.POST)
    public CommonResult getRConditions(@RequestBody JSONObject json, HttpServletRequest request) {
        RCondition rCondition = new RCondition();
        rCondition.setId(json.getInteger("id"));

        String token = request.getHeader("Authorization");
        //获取当前用户房间的rid
        String room_id = JWTUtils.getTokenStringValue(token, "room_id");
        //获取公寓的apt_id
        Integer apt_id = Integer.parseInt(JWTUtils.getTokenStringValue(token, "apt_id"));
        //获取角色id
        int rule_id = Integer.parseInt(JWTUtils.getTokenStringValue(token, "role_id"));

        String r = json.getString("rId");
        Integer a = json.getInteger("apt_id");
        if((r == null || r.equals("")) && a == null) {
            //如果是管理员
            if(rule_id == 2) {
                rCondition.setApt_id(apt_id);
                //如果是普通员工，只会查询当前员工房间的所哟设备
            } if(rule_id == 3) {
                rCondition.setRId(room_id);
                rCondition.setApt_id(apt_id);
            }
        }else {
            rCondition.setRId(r);
            rCondition.setApt_id(a);
        }
        rCondition.setECharge(json.getBigDecimal("eCharge"));
        rCondition.setWCharge(json.getBigDecimal("wCharge"));
        rCondition.setETime(json.getString("eTime"));
        rCondition.setWTime(json.getString("wTime"));
        rCondition.setHygiene(json.getInteger("hygiene"));
        rCondition.setHygieneLog(json.getString("hygieneLog"));
        rCondition.setTime(json.getString("time"));
        rCondition.setStatus(1);
        PageHelper.startPage(json.getInteger("pageNum"), json.getInteger("pageSize"));
        List<RCondition> rDevices = rConditionService.getRConditions(rCondition);
        PageInfo<RCondition> pageInfo = new PageInfo<>(rDevices);
        return new CommonResult(CommonParams.SUCCESS, "查询成功！", pageInfo);
    }

    //跟据房间号 id, rId, status 来查询
    @RequestMapping(value = "/getRConditionByIdRIdStatus", method = RequestMethod.GET)
    public CommonResult getRConditionByIdRIdStatus(@RequestParam(value = "id",required = false) Integer id,
                                                   @RequestParam(value = "rId", required = false) Integer rId,
                                                   @RequestParam(value = "status", required = false)Integer status) {
        return new CommonResult(CommonParams.SUCCESS, "查询成功！", rConditionService.getRConditionByIdRIdStatus(id, rId, status));
    }

    //插入一条房间情况的信息
    @RequestMapping(value = "/insertRCondition", method = RequestMethod.POST)
    public CommonResult insertRCondition(@RequestBody JSONObject json) {
        RCondition rCondition = new RCondition();
        rCondition.setId(json.getInteger("id"));
        rCondition.setRId(json.getString("rId"));
        rCondition.setApt_id(json.getInteger("apt_id"));
        rCondition.setECharge(json.getBigDecimal("eCharge"));
        rCondition.setWCharge(json.getBigDecimal("wCharge"));
        rCondition.setETime(json.getString("eTime"));
        rCondition.setWTime(json.getString("wTime"));
        rCondition.setHygiene(json.getInteger("hygiene"));
        rCondition.setHygieneLog(json.getString("hygieneLog"));
        rCondition.setTime(json.getString("time"));
        rCondition.setStatus(1);
        return rConditionService.insertRCondition(rCondition);
    }

    //修改一条数据的方法
    @RequestMapping(value = "/updateConditionById", method = RequestMethod.POST)
    public CommonResult updateConditionById(@RequestBody JSONObject json) {
        RCondition rCondition = new RCondition();
        rCondition.setId(json.getInteger("id"));
        rCondition.setRId(json.getString("rId"));
        rCondition.setApt_id(json.getInteger("apt_id"));
        rCondition.setECharge(json.getBigDecimal("eCharge"));
        rCondition.setWCharge(json.getBigDecimal("wCharge"));
        rCondition.setETime(json.getString("eTime"));
        rCondition.setWTime(json.getString("wTime"));
        rCondition.setHygiene(json.getInteger("hygiene"));
        rCondition.setHygieneLog(json.getString("hygieneLog"));
        rCondition.setTime(json.getString("time"));
        return rConditionService.updateConditionById(rCondition);
    }

    //删除一条数据的方法
    @RequestMapping(value = "/deleteConditionById/{id}", method = RequestMethod.POST)
    public CommonResult deleteConditionById(@PathVariable("id") Integer id) {
        return rConditionService.deleteConditionById(id);
    }
}
